package com.yuanye.gwcit.fragments;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yuanye.gwcit.R;
import com.yuanye.yeecho.base.BaseFragment;
import com.yuanye.yeecho.utils.YY;

import java.util.HashMap;
import java.util.Map;

public class KeyPressFragment extends BaseFragment {
    private String TAG = KeyPressFragment.class.getSimpleName();
    private Button btnTest;
    private TextView tvPower, tvVolumeUp, tvVolumeDown, tvBrightUp, tvBrightDown;
    private Map<String, String> result;
    private int count = 1;
    private int currentBrightness;
    private final String ACTION_ZTE = "com.zte.usmartview.fnkey.event";
    private BroadcastReceiver screenChangeReceiver;

    public KeyPressFragment() {
        setTaskName("按键测试");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.frag_key_press, container, false);
        tvPower = view.findViewById(R.id.power);
        tvVolumeUp = view.findViewById(R.id.volumeUp);
        tvVolumeDown = view.findViewById(R.id.volumeDown);
        tvBrightUp = view.findViewById(R.id.brightUp);
        tvBrightDown = view.findViewById(R.id.brightDown);
        //硬件版本号
        String hardwareVersion = YY.execShell("getprop ro.boot.board.version");
        Log.d(TAG, "hardwareVersion:"+hardwareVersion);
        if (hardwareVersion.equals("V20.0")){
            Log.d(TAG, "机型: A2145");
            view.findViewById(R.id.linearLayout).setVisibility(View.GONE);
        }else{
            Log.d(TAG, "机型: A2324");
            count = 5;
        }
        currentBrightness = Settings.System.getInt(getContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, 125);
        Log.d(TAG, "currentBrightness: "+currentBrightness);

        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
                    // 音量上键按下时的操作
                    Log.d(TAG, "音量+被按下");
                    tvVolumeUp.setBackgroundColor(Color.GREEN);
                    result.put("v_up", "yes");
                } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
                    // 音量下键按下时的操作
                    Log.d(TAG, "音量-被按下");
                    tvVolumeDown.setBackgroundColor(Color.GREEN);
                    result.put("v_down", "yes");
                }else if (keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP){
                    Log.d(TAG, "亮度+被按下");
                    tvBrightUp.setBackgroundColor(Color.GREEN);
                    result.put("b_up", "yes");
                }else if(keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN){
                    Log.d(TAG, "亮度-被按下");
                    tvBrightDown.setBackgroundColor(Color.GREEN);
                    result.put("b_down", "yes");
                }
                isTestOver();
                return true;
            }
        });
    }

    private void isTestOver() {
        if (result.keySet().size() == count){
            passAndNext();
        }
    }

    @Override
    public void execTask() {
        result = new HashMap<>();
        tvBrightUp.setBackgroundColor(Color.LTGRAY);
        tvBrightDown.setBackgroundColor(Color.LTGRAY);
        tvVolumeDown.setBackgroundColor(Color.LTGRAY);
        tvVolumeUp.setBackgroundColor(Color.LTGRAY);
        tvPower.setBackgroundColor(Color.LTGRAY);
        // 注册屏幕变化的广播接收器
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(ACTION_ZTE);
        screenChangeReceiver = new ScreenChangeReceiver();
        getContext().registerReceiver(screenChangeReceiver, filter);
    }

    @Override
    public void stopTask() {
        if (screenChangeReceiver != null){
            getContext().unregisterReceiver(screenChangeReceiver);
            screenChangeReceiver = null;
        }
    }

    class ScreenChangeReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "接收到广播："+action);
            if (action.equals(Intent.ACTION_SCREEN_OFF)){
                Log.d(TAG, "检测到屏幕被熄屏");
                tvPower.setBackgroundColor(Color.GREEN);
                result.put("screen_change", "yes");
            }else if (action.equals(ACTION_ZTE)){
                int keyCode = intent.getIntExtra("keyCode", 0);
                Log.d(TAG, "keyCode:"+keyCode);
                if (keyCode == KeyEvent.KEYCODE_VOLUME_UP){
                    tvVolumeUp.setBackgroundColor(Color.GREEN);
                    result.put("v_up", "yes");
                }else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
                    tvVolumeDown.setBackgroundColor(Color.GREEN);
                    result.put("v_down", "yes");
                }else if(keyCode == KeyEvent.KEYCODE_BRIGHTNESS_UP){
                    tvBrightUp.setBackgroundColor(Color.GREEN);
                    result.put("b_up", "yes");
                }else if(keyCode == KeyEvent.KEYCODE_BRIGHTNESS_DOWN){
                    tvBrightDown.setBackgroundColor(Color.GREEN);
                    result.put("b_down", "yes");
                }
            }
            isTestOver();

        }
    };
}
